<?php

class admin extends Controller {

	function admin()
	{
		parent::Controller();
		date_default_timezone_set ("Asia/Bangkok"); 	
		if(!$this->user->is_login())
		{
			redirect('login', 'location'); 
		}
        //$this->user->protection();
		$this->form_validation->set_message('required', 'กรุณาระบุ %s');
		$this->form_validation->set_message('is_natural_no_zero', 'กรุณาระบุ %s');
		//$this->output->enable_profiler(TRUE);
	}
	function approve_popup($allocation_item_id,$material_id = 0)
	{
		$data["allocation_item_id"] = $allocation_item_id;
		$data["material_id"] = $material_id;
		if($material_id==0)
		{
			$this->form_validation->set_rules('code',  'code', 'trim|required');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				
				$this->template->load('admin/popup_theme',"admin/approve_popup",$data);
			
			}
			else
			{ 	
				$values = array();
				$values = $_POST;
				$values["create_date"] =date("Y-m-d H:i:s");
				$values["allocation_item_id"] = $allocation_item_id;
				$values["amount"] = 1; 
			    $this->db->insert('sub_allocation_items',$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
			}
		}
		
	}
	function have_code()
	{
	
	}
	function request_detail($id)
	{
		$data["id"] = $id;
		$this->template->load('admin/themes',"admin/request_detail",$data);
		
	}
	function request_list()
	{
		$this->template->load('admin/themes',"admin/request_list");
	}
	function teacher_request()
	{
		$this->form_validation->set_rules('detail',  'รายละเอียด', 'trim|required');
			
			
		if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/teacher_request");
			
			}
			else
			{ 	
				$values = array();
				$user = $this->user->get();
				$values["user_id"] = $user["user_id"];
				$values["detail"] = $_POST["detail"];
				$values["allocation_date"] = date("Y-m-d h:i:s");
				$this->db->insert("allocations",$values);
				//$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				$m = $this->db->query("select max(allocation_id) as id from allocations")->result_array();
				$cat_ids = $this->session->userdata("cat_ids");
				
				foreach($cat_ids as $key=>$row)
				{
				 $item = array();
				 $item["allocation_id"] = $m[0]["id"];
				 $item["category_id"] = $key;
				 $item["amount"] = $row["amount"];
				 $item["remark"] = $row["remark"];
				 $this->db->insert("allocation_items",$item);
				}
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				$this->session->unset_userdata("cat_ids");
				redirect("admin/request_list","location");
			}
			
	}
	function allocation_add()
	{
			$this->form_validation->set_rules('user_id',  'ชื่อห้อง', 'trim|required');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/allocation_add");
			
			}
			else
			{ 	
				$this->db->insert("place",$_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
			}
			
				
	}
	function material_popup()
	{
		$this->template->load('admin/popup_theme','admin/material_popup');
	}
	function maintain_view($id)
	{
		 		$rows = $this->db->get("maintain_views")->result_array();
                $data["row"] = $rows[0];
				$this->template->load('admin/themes',"admin/maintain_view",$data);
	}
	function maintain_request_finish_list($offset = 0)
	{
				$base = $this->user->current_controller();
			
				$sql ="select * from maintain_history_views  where status_id = 4";
				$data =array();
				$this->user->save_action();
				$this->_page_query($sql,$base,"admin/maintain_request_finish_list",$offset,$data,true,30);
	}
	function maintain_finish($id)
	{
			$this->form_validation->set_rules('category_id',  'ชนิดปัญหาที่พบ', 'trim');
			$this->form_validation->set_rules('subject',  'หัวเรื่อง', 'trim');
			$this->form_validation->set_rules('ramark',  'รายละเอียดปัญหา', 'trim');
			$this->db->where("mh_id",$id);
			
			if ($this->form_validation->run() == FALSE)
			{
				
			    $rows = $this->db->get("maintain_history_views")->result_array();
                $data["row"] = $rows[0];
				$this->template->load('admin/themes',"admin/maintain_finish",$data);
			}
			else
			{
				$values = $_POST;
				$values["finish_date"] =   DateTime::createFromFormat('d/m/Y',$_POST["maintain_date"])->format('Y-m-d');
				$values['status_id'] =4;
				$user = $this->user->get();
				$values["finish_approve_by"] = $user["firstname"]." ".$user["lastname"];
				$this->db->update("maintain_history",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
                $this->user->back_action();

               
			}
	}
	function maintain_request_process_list($offset=0)
	{
				$base = $this->user->current_controller();
			
				$sql ="select * from maintain_history_views  where status_id = 2";
				$data =array();
				$this->user->save_action();
				$this->_page_query($sql,$base,"admin/maintain_request_list",$offset,$data,true,30);
	}
	function maintain_request_list($offset=0)
	{
				$base = $this->user->current_controller();
				$condition ="";
				$user = $this->user->get();
				if(!$this->user->is_role(1))
				{

					$condition = " and  user_id =".$user["user_id"];
				}
				
				$sql ="select * from maintain_views  where 1 $condition";
				$data =array();
				$this->user->save_action();
				$this->_page_query($sql,$base,"admin/maintain_request_list",$offset,$data,true,30);
	}
	function maintain_request()
	{
			$this->form_validation->set_rules('code',  'code', 'trim|required|callback_ref_code_check');
			$this->form_validation->set_rules('subject',  'หัวเรื่อง', 'trim|required');
			$this->form_validation->set_rules('remark',  'รายละเอียดปัญหา', 'trim|required');
			if ($this->form_validation->run() == FALSE)
			{
				
				
				$this->template->load('admin/themes',"admin/maintain_request");
			}
			else
			{
				$values = $_POST;
				//$values["maintain_date"] =   DateTime::createFromFormat('d/m/Y',$_POST["maintain_date"])->format('Y-m-d');
				$values['create_date'] =date("y/m/d H:i:s");
				//$values['request_date'] =date("y/m/d H:i:s");
				//$values["ref_code"] = $this->p->getcode("F");
				$user = $this->user->get();
				$values['status_id'] = 1;
				$values["user_id"] = $user["user_id"];
				//$values["request_by"] = $user["firstname"]." ".$user["lastname"];
				
				$this->db->insert("maintains",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
                $this->user->back_action();

               
			}
	}
	function maintain_request_delete($id)
	{
			$this->form_validation->set_rules('category_id',  'ชนิดปัญหาที่พบ', 'trim');
			$this->form_validation->set_rules('subject',  'หัวเรื่อง', 'trim');
			$this->form_validation->set_rules('ramark',  'รายละเอียดปัญหา', 'trim');
			$this->db->where("mh_id",$id);
			if ($this->form_validation->run() == FALSE)
			{
				
				
    			$rows = $this->db->get("maintain_history_views")->result_array();
                $data["row"] = $rows[0];
				$this->template->load('admin/themes',"admin/maintain_request_delete",$data);
			}
			else
			{
				
				
				$this->db->delete("maintain_history");
				$this->user->set_message("","ลบเรียบร้อยแล้ว","success");	
                $this->user->back_action();

               
			}
	}
	function m_maintain_add($id)
	{
			//is_natural_no_zero
			$this->form_validation->set_rules('company_id',  'บิริษัท/ผู้รับเหมาส่งซ่อม', 'trim|is_natural_no_zero');
			$this->form_validation->set_rules('price',  '', 'trim');
			$this->form_validation->set_rules('budget_id',  '', 'trim');
			$this->form_validation->set_rules('maintain_date',  'วันทีซ่อม', 'trim|required');
			$this->form_validation->set_rules('responsible',  'ผู้รับผิดชอบ', 'trim');
			$this->form_validation->set_rules('remark2',  'รายละเอียดงานซ่อม', 'trim');
			$this->db->where("mh_id",$id);
			if ($this->form_validation->run() == FALSE)
			{
				
				
    			$rows = $this->db->get("maintain_history_views")->result_array();
                $data["row"] = $rows[0];
				$this->template->load('admin/themes',"admin/m_maintain_add",$data);
			}
			else
			{
				$values = $_POST;
				$values["maintain_date"] =   DateTime::createFromFormat('d/m/Y',$_POST["maintain_date"])->format('Y-m-d');
				$values['last_modify'] =date("Y-m-d H:i:s");
				$values["status_id"] =2; 
				$this->db->update("maintain_history",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
               	$this->user->back_action();

               
			}
	}
	
    function search()
    {


    	
    		$this->form_validation->set_rules('category_id',  'category', 'trim');
			$this->form_validation->set_rules('company_id',  'compay', 'trim');
			$this->form_validation->set_rules('budget_id',  'budget_id', 'trim');
			$this->form_validation->set_rules('type_id',  'type_id', 'trim');
    		if ($this->form_validation->run() == FALSE)
			{
				
				if(!isset($_POST))
				{
					$this->_init_filter();
				
				}
				
				$this->template->load('admin/themes',"admin/search",null);
			}
			else
			{

               
	            $filter = array();
				$filter = $_POST;
		
				$this->session->set_userdata("filter",$filter);
		
				
				$this->user->save_action();
				
				$this->template->load('admin/themes',"admin/search");
               
			}

    }
    function ref_code_check($str)
    {
        if ($str == '')
		{

			return true;
		}
		else
		{
			$this->form_validation->set_message('ref_code_check', 'เลขที่วัสดุ-ครุถัณฑ์นี้ไม่มีในระบบ');
            $code = $str;
			$sql = "select * from material_items where code = '$code'";
            $rows  =  $this->db->query($sql)->result_array();
            if(count($rows))
            {
                return true;
            }
            else
            {
                return false;
            }

		}
    }
    function material_search()
    {
        
    }
	function csv()
	{
		$this->load->dbutil();

		$query = $this->db->query("SELECT * FROM roles");
		
		echo $this->dbutil->csv_from_result($query); 
	}
	function room_add()
	{
			$this->form_validation->set_rules('place_name',  'ชื่อห้อง', 'trim|required');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/room_add");
			
			}
			else
			{ 	
				$this->db->insert("place",$_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				redirect('admin/room', 'location');
				
			}
	}
	function room_edit($id)
	{
			$this->form_validation->set_rules('place_name',  'ชื่อห้อง', 'trim|required');
			
			$this->db->where("place_id",$id);
			
			if ($this->form_validation->run() == FALSE)
			{
				$rows = $this->db->get("place")->result_array();
				$data["row"] = $rows[0];
				$this->template->load('admin/themes',"admin/room_edit",$data);
			
			}
			else
			{ 	
				
				$this->db->update("place",$_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				redirect('admin/room', 'location');
				
			}
	}
	function room_delete($id)
	{
	}
	function room()
	{
		$header =
		 array( 
			  array("class"=>"center","name"=>"place_name","label"=>"ชื่อห้อง/สถานที่"),
			 
			  array("class"=>"center edit","name"=>"place_id","label"=>"แก้ไข"),
			  array("class"=>"center delete","name"=>"place_id","label"=>"ลบ")
	 			);
		$rows = $this->db->query("select * from place")->result_array();
		$data["header"] = $header; 
		$data["rows"] = $rows;
		$this->template->load('admin/themes',"admin/room",$data);
	}
	
	function _page_query($sql,$base,$view="",$offset=0,$data= array(),$show =true,$limit = 9)
	{
		     
		$config['base_url'] = base_url()."index.php/$base/";
		$config['per_page'] = $limit;
		$config['total_rows'] = count($this->db->query($sql)->result_array());
		$this->pagination->initialize($config);
		
		$data['page_link'] =$this->pagination->create_links();
		$data['total'] = ceil( $config['total_rows']/$limit) ;
		
		$data['current_page'] = 1;
		
		if($offset){
		$data['current_page'] = ($offset/$limit)+1; 
		}
		
		$sql .=" limit $offset,$limit"; 
		$data['page'] =$this->pagination;
		
		$data['rows'] = $this->db->query($sql)->result_array();
		
		
		
		$data['paging'] = $this->load->view("paging",$data,true);	
		
		
		if($show){
		$this->template->load("admin/themes",$view,$data);
		}else
		{
			return $data;
		}
		
	}
	function m_request_detail($id)
	{
		$data["id"]  = $id;
		$this->template->load('admin/themes',"admin/m_request_detail",$data);
	}
	function m_request_list()
	{
		$base = $this->user->current_controller();
		
		
		
		
	    $user = $this->user->get();
		$sql ="SELECT material_requests.mr_id, 
		material_requests.detail, 
		material_requests.place_id, 
		material_requests.user_id, 
		date_format(material_requests.create_date,'%d/%m/%Y') as create_date, 
		users.firstname, 
		users.lastname, 
		users.position, 
		place.place_name
	FROM material_requests INNER JOIN users ON material_requests.user_id = users.user_id
		 INNER JOIN place ON material_requests.place_id = place.place_id where 1";
		$this->_page_query($sql,$base,$base,0,null,true,30);
	}
	function m_request_add()
	{
	
			$this->form_validation->set_rules('cat_values[]',  '', 'trim|required');
		
			
			
			if ($this->form_validation->run() == FALSE)
			{
				//$this->user->save_action();
				$this->template->load('admin/themes',"admin/m_request_add");
			
			}
			else
			{ 	
				
				$values = array();
				$user = $this->user->get();
				$values["user_id"] = $user["user_id"];
				$values["detail"] = $_POST["detail"];
				$values["place_id"] = $_POST["place_id"];
				$values["create_date"] = date("Y-m-d h:i:s");
				$this->db->insert("material_requests",$values);
				
				$m = $this->db->query("select max(mr_id) as id from material_requests")->result_array();
				$cat_ids = $_POST["cat_values"];
				
				foreach($cat_ids as $key=>$row)
				{
					 $item = array();
					 $item["mr_id"] = $m[0]["id"];
					 $item["category_id"] = $key;
					 $item["amount"] = $row["amount"];
					 if( $item["amount"]>1)
					 {
						 $this->db->insert("material_request_items",$item);
					 }
				}
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				//redirect("admin/request_list","location");
			
				//$this->db->insert("material_requests",$values);
				//$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
			
				//$this->user->back_action();
				//redirect("admin/m_request_list","location");
				
			}
	}
	function index()
	{
		$this->template->load('admin/themes',"home",null);
		$this->_init_filter();
		
		//redirect('admin/request_list', 'location'); 
	}
	function _init_filter()
	{
		
		$filter = array();
		$filter["budget_id"] = 0;
		$filter["category_id"] = 0;
		$filter["company_id"] = 0;
		$filter["type_id"] = 0;
		
		$this->session->set_userdata("filter",$filter);
		
	}
	function category_edit($id)
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('category_name',  'ชื่อหมวด', 'trim|required');
			$this->form_validation->set_rules('cat_base_id',  'ชนิด', 'trim|required');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$rows = $this->db->query("select * from material_categories where category_id =$id")->result_array();
				$data['row'] = $rows[0];
				$this->template->load('admin/themes',"admin/category_edit",$data);
			}
			else
			{ 	
				
				$this->db->where('category_id', $id);
				$this->db->update('material_categories', $_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				redirect('admin/categories', 'location');
				
			}
	}
	
	function category_add()
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('category_name',  'ชื่อหมวด', 'trim|required');
			$this->form_validation->set_rules('cat_base_id',  'ชนิด', 'trim|required');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/category_add",null);
			}
			else
			{ 	
				$this->db->insert("material_categories",$_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				redirect('admin/categories', 'location');
				
			}
	}
	function user_add()
	{
			
			$this->form_validation->set_rules('username',  'ชื่อผู้ใช้งาน', 'trim|required');
			$this->form_validation->set_rules('password', 'รหัสผ่าน', 'trim|required');
			$this->form_validation->set_rules('firstname', 'ชื่อ', 'trim|required');
			$this->form_validation->set_rules('lastname', 'นามสกุล', 'trim|required');
			$this->form_validation->set_rules('position', 'ตำแหน่ง', 'trim');
			$this->form_validation->set_rules('email', 'อีเมล์', 'trim|required');
			$this->form_validation->set_rules('role_id', 'ระดับผู้ใช้งาน', 'trim|required|is_natural_no_zero');
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/user_add",null);
			}
			else
			{ 	
				$values = $_POST;
				$values["password"] = md5($values["password"]);
				$this->db->insert("users",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
					
				redirect('admin/users', 'location');
				
			}
	}
	function user_delete($id)
	{
	
			$this->form_validation->set_rules('user_id', '', 'trim');
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->db->where("user_id",$id);
				$data["user"] = $this->db->get("users")->result_array();
				$this->template->load('admin/themes',"admin/user_delete",$data);
			}
			else
			{ 	
				
				$this->db->where("user_id",$id);
				$this->db->delete("users");
				$this->user->set_message("","ลบกเรียบร้อยแล้ว","success");
			
				redirect('admin/users', 'location');
				
			}
	}
	function pass_edit($id)
	{
	$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			
			$this->form_validation->set_rules('password', '', 'trim|required');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->db->where("user_id",$id);
				$data["user"] = $this->db->get("users")->result_array();
				$this->template->load('admin/themes',"admin/pass_edit",$data);
			}
			else
			{ 	
				$values = $_POST;
				$values["password"] = md5($values["password"]);
				$this->db->where("user_id",$id);
				$this->db->update("users",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				redirect('admin/users', 'location');
				
			}
			
	}
    function pass_edit2()
	{
	        $this->form_validation->set_message('required', 'กรุณาระบุ %s');

			$this->form_validation->set_rules('password', '', 'trim|required');


			if ($this->form_validation->run() == FALSE)
			{
				$this->db->where("user_id",$this->user->get_user_id());
				$data["user"] = $this->db->get("users")->result_array();
				$this->template->load('admin/themes',"admin/pass_edit2",$data);
			}
			else
			{
				$values = $_POST;
				$values["password"] = md5($values["password"]);
				$this->db->where("user_id",$this->user->get_user_id());
				$this->db->update("users",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");

				redirect('admin/pass_edit2', 'location');

			}

	}
	function user_edit($id)
	{
			//$this->form_validation->set_rules('username',  'ชื่อผู้ใช้งาน', 'trim|required');
			//$this->form_validation->set_rules('password', 'รหัสผ่าน', 'trim|required');
			$this->form_validation->set_rules('firstname', 'ชื่อ', 'trim|required');
			$this->form_validation->set_rules('lastname', 'นามสกุล', 'trim|required');
			
			$this->form_validation->set_rules('position', 'ตำแหน่ง', 'trim');
			$this->form_validation->set_rules('email', 'อีเมล์', 'trim|required');
			$this->form_validation->set_rules('tel', '', 'trim');
			$this->form_validation->set_rules('role_id', 'ระดับผู้ใช้งาน', 'trim|required');
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->db->where("user_id",$id);
				$data["user"] = $this->db->get("users")->result_array();
				$this->template->load('admin/themes',"admin/user_edit",$data);
			}
			else
			{ 	
				$values = $_POST;
				$this->db->where("user_id",$id);
				$this->db->update("users",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
			
				//print_r($_POST);
				redirect('admin/users', 'location');
				
			}
	}
    function user_edit2()
	{
			//$this->form_validation->set_rules('username',  'ชื่อผู้ใช้งาน', 'trim|required');
			//$this->form_validation->set_rules('password', 'รหัสผ่าน', 'trim|required');
			$this->form_validation->set_rules('firstname', 'ชื่อ', 'trim|required');
			$this->form_validation->set_rules('lastname', 'นามสกุล', 'trim|required');

			$this->form_validation->set_rules('position', 'ตำแหน่ง', 'trim');
			$this->form_validation->set_rules('email', 'อีเมล์', 'trim|required');
			$this->form_validation->set_rules('tel', '', 'trim');
			//$this->form_validation->set_rules('role_id', 'ระดับผู้ใช้งาน', 'trim|required');
			$this->db->where("user_id",$this->user->get_user_id());
			if ($this->form_validation->run() == FALSE)
			{
                $rows =  $this->db->get("users")->result_array();
				$data["row"] =$rows[0];
				$this->template->load('admin/themes',"admin/user_edit2",$data);
			}
			else
			{
				$values = $_POST;

				$this->db->update("users",$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");

				redirect('admin/user_edit2', 'location');

			}
	}
	function category_delete($id)
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('category_id',  '', 'trim|required');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$data['cat'] = $this->db->query("select * from material_categories where category_id =$id")->result_array();
				$this->template->load('admin/themes',"admin/category_delete",$data);
			}
			else
			{ 	
				$this->db->where('category_id', $id);
				$this->db->delete('material_categories');
				$this->user->set_message("","ลบเรียบร้อยแล้ว","success");	
				
				redirect('admin/categories', 'location');
				
			}
	}
	function categories()
	{
		
		$this->template->load('admin/themes',"admin/categories",null);
	}
	function contacts()
	{
		$this->template->load('admin/themes',"admin/contacts",null);
	}
	function material_view($id,$id2=0)
	{
		$this->db->where("material_id",$id);
		$rows = $this->db->get("inventory_views")->result_array();
		$data["row"] = $rows[0];
		$data["id2"] = $id2;
		$this->template->load("admin/themes","admin/m_detail",$data);
		
	}
	function item_update_status()
	{
		
	$this->form_validation->set_rules('code',  'code', 'trim|required|callback_ref_code_check');
			
			
		if ($this->form_validation->run() == FALSE)
		{
				
				$this->template->load("admin/themes","admin/item_update_status");
			
		}
		else
		{ 	
				$values = array();
				$values = $_POST;
				$values["exp_date"] =date("Y-m-d");
				$values["status_id"] = 2;
				$this->db->where("code",$_POST["code"]);
			    $this->db->update('material_items',$values);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				redirect("admin/item_update_status","location");
		}
		
		
	
	}
	function m_exp()
	{
		$this->template->load("admin/themes","admin/m_exp");
	}
	function fine_code($code)
	{
		$this->db->where("code",str_replace("-", "/", $code));
		$rows = $this->db->get("material_items")->result_array();
		///print_r($rows);
		redirect("admin/material_view/".$rows[0]["material_id"],"location");
	}
	
	function material_filter()
	{
		$id = $_POST["category_id"];
		$filter = $this->session->userdata("filter");
		$filter["category_id"] = $id;
		$this->session->set_userdata("filter",$filter);
		redirect('admin/materials', 'location');
		
	}
	function material_filter2()
	{
		$id = $_POST["category_id"];
		$filter = $this->session->userdata("filter");
		$filter["category_id"] = $id;
		$this->session->set_userdata("filter",$filter);
		redirect('admin/materials2', 'location');
		
	}
	function materials($offset=0)
	{
		$filter = $this->session->userdata("filter");
		$condition ="";
		if($filter["category_id"])
		{
		$condition = " and category_id = ".$filter["category_id"];
		
		}
		$sql = "select * from material_views where type_id =2 $condition "; 
		$base = "admin/materials";
		
		$this->_page_query($sql,$base,"admin/materials",$offset,array(),true,50);
		
		
	}
	function materials2($offset=0)
		{
			$filter = $this->session->userdata("filter");
			$condition ="";
			if($filter["category_id"])
			{
			$condition = " and category_id = ".$filter["category_id"];
			
			}
			$sql = "select * from material_views m 	where type_id = 1 $condition "; 
			$base = "admin/materials2";
			
			$this->_page_query($sql,$base,"admin/materials2",$offset,array(),true,50);
			
	
		}
	function material_add2()
	{
	
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('name',  'ชื่อ', 'trim|required');
			
			$this->form_validation->set_rules('category_id', 'หมวดสินค้า', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('budget_id', 'ชนิดงบประมาณ', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('company_id', 'บริษัท', 'trim|required|is_natural_no_zero');
			
			$this->form_validation->set_rules('brand', 'ยีห้อ', 'trim|required');
			$this->form_validation->set_rules('model', '', 'trim');
			$this->form_validation->set_rules('buy_price', 'ราคาซื้อ', 'trim');
			$this->form_validation->set_rules('buy_date', 'วันที่ซื้อ', 'trim');
			$this->form_validation->set_rules('detail', '', 'trim');
			$this->form_validation->set_rules('thumbnail', '', 'trim');
			$this->form_validation->set_rules('warranty', '', 'trim');
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/material_add2",null);
				//$this->user->save_action();
			}
			else
			{ 	
				$query = $_POST;
				$query['type_id'] = 1;
				$query['last_modify'] =date("y/m/d H:i:s");
				$query['create_date'] =date("y/m/d H:i:s");
				//$query["ref_code"] = $this->p->getcode();
				$this->db->insert("material_registers",$query);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
				//$this->user->back_action();
				$rows = $this->db->query("select max(material_id) as id from material_registers")->result_array();
				
				redirect("admin/material_add_item2/".$rows[0]["id"],"location");
			}
		
		
	
	}
	function material_edit2($id)
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('name',  'ชื่อ', 'trim|required');
			
			$this->form_validation->set_rules('category_id', 'หมวดสินค้า', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('budget_id', 'ชนิดงบประมาณ', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('company_id', 'บริษัท', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('place_id', 'ชื่อสถานที่', 'trim|required|is_natural_no_zero');
			
			$this->form_validation->set_rules('brand', 'ยีห้อ', 'trim|required');
			$this->form_validation->set_rules('model', '', 'trim');
			$this->form_validation->set_rules('buy_price', 'ราคาซื้อ', 'trim');
			$this->form_validation->set_rules('buy_date', 'วันที่ซื้อ', 'trim');
			$this->form_validation->set_rules('detail', '', 'trim');
			$this->form_validation->set_rules('thumbnail', '', 'trim');
			$this->form_validation->set_rules('warranty', '', 'trim');
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->db->where("material_id",$id);
				$rows = $this->db->get("inventory_views")->result_array();
				$data["row"] = $rows[0];
				$this->user->save_action();
				$this->template->load('admin/themes',"admin/material_edit2",$data);
			}
			else
			{ 	
				$query = $_POST;
				
				$query['last_modify'] =date("y/m/d H:i:s");
				$query['create_date'] =date("y/m/d H:i:s");
				$this->db->where("material_id",$id);
				$this->db->update("materials",$query);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
				
				$this->user->back_action();
			}
	}
	function material_add()
	{
		
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('name',  'ชื่อ', 'trim|required');
			
			$this->form_validation->set_rules('category_id', 'หมวดสินค้า', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('budget_id', 'ชนิดงบประมาณ', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('company_id', 'บริษัท', 'trim|required|is_natural_no_zero');
			
			$this->form_validation->set_rules('brand', 'ยีห้อ', 'trim|required');
			$this->form_validation->set_rules('model', '', 'trim');
			$this->form_validation->set_rules('buy_price', 'ราคาซื้อ', 'trim');
			$this->form_validation->set_rules('buy_date', 'วันที่ซื้อ', 'trim');
			$this->form_validation->set_rules('detail', '', 'trim');
			$this->form_validation->set_rules('thumbnail', '', 'trim');
			$this->form_validation->set_rules('warranty', '', 'trim');
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/material_add",null);
				//$this->user->save_action();
			}
			else
			{ 	
				$query = $_POST;
				$query['type_id'] = 2;
				$query['last_modify'] =date("y/m/d H:i:s");
				$query['create_date'] =date("y/m/d H:i:s");
				$date = DateTime::createFromFormat('d/m/Y', $_POST["buy_date"]);

				$query["buy_date"] = $date->format('Y-m-d');
				//$query["ref_code"] = $this->p->getcode();
				$this->db->insert("material_registers",$query);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
				//$this->user->back_action();
				$rows = $this->db->query("select max(material_id) as id from material_registers")->result_array();
				
				redirect("admin/material_add_item/".$rows[0]["id"],"location");
			}
		
		
		
	}
	function material_add_finish($id)
	{
		$this->db->where("material_id",$id);
		$rows = $this->db->get("material_views")->result_array();
		$data["row"] = $rows[0];	
		$this->template->load('admin/themes',"admin/material_add_finish",$data);
	}
function material_add_finish2($id)
	{
		$this->db->where("material_id",$id);
		$rows = $this->db->get("material_views")->result_array();
		$data["row"] = $rows[0];	
		$this->template->load('admin/themes',"admin/material_add_finish2",$data);
	}
	function code_check($code)
	{	
		 
		 $rows = $this->db->query("select count(*) as c from material_items where code='$code'")->result_array();
		 $this->form_validation->set_message('code_check', 'มีเลขที่วัสดุ-ครุภัณฑ์นี้อยุ่ในระบบแล้ว');
		
		 if(!$rows[0]['c'])
		 {
		  	return true;
		 }
		 else
		 {
		  	return false;
		 }
		}
	function material_add_item($id=0)
	{
	
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('code',  'รหัสครุภัณฑ์', 'trim|required|callback_code_check');
			$this->form_validation->set_rules('remark',  '', 'trim');
			
			
			
			

			
			$this->db->where("material_id",$id);
			if ($this->form_validation->run() == FALSE)
			{
				$rows = $this->db->get("material_views2")->result_array();
				$data["row"] = $rows[0];
				
				$this->template->load('admin/themes',"admin/material_add_item",$data);
				//$this->user->save_action();
			}
			else
			{ 	
				$query = $_POST;
				$query["material_id"] = $id;
				$query['register_date'] =date("y/m/d H:i:s");
				$this->db->insert("material_items",$query);
				
				redirect("admin/material_add_item/".$id,"location");
			
			}
	}
function material_add_item2($id=0)
	{
	
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('code',  'เลขที่วัสดุ', 'trim|required|callback_code_check');
			$this->form_validation->set_rules('amount',  'จำนวน', 'trim|required');
			$this->form_validation->set_rules('remark',  '', 'trim');
			
			
			
			

			
			$this->db->where("material_id",$id);
			if ($this->form_validation->run() == FALSE)
			{
				$rows = $this->db->get("material_views2")->result_array();
				$data["row"] = $rows[0];
				
				$this->template->load('admin/themes',"admin/material_add_item2",$data);
				//$this->user->save_action();
			}
			else
			{ 	
				$query = $_POST;
				$query["material_id"] = $id;
				$query['register_date'] =date("y/m/d H:i:s");
				$query["code"] = $this->p->barcode("M2");
				$query["barcode"] = "100000".$this->p->barcode("M2");
				$this->db->insert("material_items",$query);
				
				redirect("admin/material_add_item2/".$id,"location");
			
			}
	}
	function material_edit($id)
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('name',  'ชื่อ', 'trim|required');
			
			$this->form_validation->set_rules('category_id', 'หมวดสินค้า', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('budget_id', 'ชนิดงบประมาณ', 'trim|required|is_natural_no_zero');
			$this->form_validation->set_rules('company_id', 'บริษัท', 'trim|required|is_natural_no_zero');
			
			
			$this->form_validation->set_rules('brand', 'ยีห้อ', 'trim|required');
			$this->form_validation->set_rules('model', '', 'trim');
			$this->form_validation->set_rules('buy_price', 'ราคาซื้อ', 'trim');
			$this->form_validation->set_rules('buy_date', 'วันที่ซื้อ', 'trim');
			$this->form_validation->set_rules('detail', '', 'trim');
			
			$this->form_validation->set_rules('warranty', '', 'trim');
			if ($this->form_validation->run() == FALSE)
			{
				$rows = $this->db->query("select * from material_views where material_id = $id")->result_array();
				$data["row"] = $rows[0];    
				$this->template->load('admin/popup_theme',"admin/material_edit",$data);
			}
			else
			{ 	
				$this->db->where('material_id', $id);
				
				$values = $_POST;
				//$values["buy_date"] = date('m/d/Y', strtotime($rss)); ;
				$this->db->update("material_registers",$values);
				
				$buy_date = $_POST["buy_date"];
				$this->db->query("update material_registers set buy_date=STR_TO_DATE('$buy_date','%d/%m/%Y') where material_id = $id");
				
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
				
				//echo $this->db->truncate(); 
				$this->template->load("admin/popup_theme","admin/window_close");
			}
		
	}
	
	function maintain_add()
	{
			
			$this->form_validation->set_rules('maintain_name',  'ชื่องานซ่อม', 'trim|required');
			$this->form_validation->set_rules('remark',  '', 'trim');
			$this->form_validation->set_rules('category_id',  'หมวด', 'trim|required');
			
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/maintain_add",null);
			}
			else
			{ 	
				$this->db->insert("maintain",$_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				redirect('admin/maintains', 'location');
				
			}
	}
	function maintain_edit($id)
	{
			
			$this->form_validation->set_rules('subject',  'ชื่องานซ่อม', 'trim|required');
			$this->form_validation->set_rules('remark2',  '', 'trim');
			$this->form_validation->set_rules('finish_date',  '', 'trim');
			$this->form_validation->set_rules('stats_id',  '', 'trim');
			$this->form_validation->set_rules('price',  '', 'trim');
			$this->form_validation->set_rules('warranty',  '', 'trim');
			
			
			
			$this->db->where("maintain_id",$id);
			if ($this->form_validation->run() == FALSE)
			{
				$rows = $this->db->get("maintain_views")->result_array();
				$data["row"] = $rows[0];
				$this->template->load('admin/popup_theme',"admin/maintain_edit",$data);
			}
			else
			{ 	
				$values = $_POST;
				$values["finish_date"] = DateTime::createFromFormat('d/m/Y', $_POST["finish_date"])->format('Y-m-d');
				
				$this->db->update("maintains",$values);
				//$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				$this->template->load("admin/popup_theme","admin/window_close");
				//redirect('admin/maintains', 'location');
				
			}
	}
function maintain_delete($id)
	{
			
			$this->form_validation->set_rules('maintain_id',  '', 'trim');
			
			
			
			$this->db->where("maintain_id",$id);
			if ($this->form_validation->run() == FALSE)
			{
				$rows = $this->db->get("maintain")->result_array();
				$data["row"] = $rows[0];
				$this->template->load('admin/themes',"admin/maintain_delete",$data);
			}
			else
			{ 	
				$this->db->delete("maintain");
				$this->user->set_message("","ลบรียบร้อยแล้ว","success");
				
				redirect('admin/maintains', 'location');
				
			}
	}
	function maintains()
	{
		$header =
		 array( 
			  array("class"=>"center width200","name"=>"category_name","label"=>"ชื่อหมวด"),
			  array("class"=>"center","name"=>"maintain_name","label"=>"งานซ่อม"),
			  array("class"=>"center","name"=>"remark","label"=>"หมายเหตุ"),
			  array("class"=>"center edit","name"=>"maintain_id","label"=>"แก้ไข"),
			  array("class"=>"center delete","name"=>"maintain_id","label"=>"ลบ")
	 			);
		$rows = $this->db->query("select * from maintain m left join material_categories c on c.category_id = m.category_id")->result_array();
		$data["header"] = $header; 
		$data["rows"] = $rows;
		$this->template->load('admin/themes',"admin/maintains",$data);
	}
	function budgets()
	{
		$header =
		 array( 
			  array("class"=>"left width200","name"=>"budget_name","label"=>"ชื่องบประมาณ"),
			  array("class"=>"left","name"=>"year","label"=>"ปีการศึกษา"),
			  array("class"=>"center edit","name"=>"budget_id","label"=>"แก้ไข"),
			  array("class"=>"center delete","name"=>"budget_id","label"=>"ลบ")
	 			);
		$rows = $this->db->query("select * from budgets")->result_array();
		$data["header"] = $header; 
		$data["rows"] = $rows;
		$this->template->load('admin/themes',"admin/budget",$data);
	}
	function budget_add()
	{
			
			$this->form_validation->set_rules('budget_name',  'ชื่อชนิดงบประมาณ', 'trim|required');
			$this->form_validation->set_rules('year',  'ปีการศึกษา', 'trim|required');
			
			
			
			if($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',"admin/budget_add",null);
			}
			else
			{ 	
				$this->db->insert("budgets",$_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
				
				redirect('admin/budgets', 'location');
				
			}
	}
	function budget_edit($id)
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('budget_name',  'ชื่อชนิดงบประมาณ', 'trim|required');
			$this->form_validation->set_rules('year',  'ปีการศึกษา', 'trim|required');
			
			
			$this->db->where("budget_id",$id);
			
				if ($this->form_validation->run() == FALSE)
				{
					$rows = $this->db->get("budgets")->result_array();
					$data["row"] = $rows[0];
					$this->template->load('admin/themes',"admin/budget_edit",$data);
				}
				else
				{ 	
					$this->db->update("budgets",$_POST);
					$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
					
					redirect('admin/budgets', 'location');
					
				}
	}
	function budget_delete($id)
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('budget_name',  'ชื่อชนิดงบประมาณ', 'trim');
			
			
			
			$this->db->where("budget_id",$id);
			
				if ($this->form_validation->run() == FALSE)
				{
					$rows = $this->db->get("budgets")->result_array();
					$data["row"] = $rows[0];
					$this->template->load('admin/themes',"admin/budget_delete",$data);
				}
				else
				{ 	
					$this->db->delete("budgets");
					$this->user->set_message("","ลบเรียบร้อยแล้ว","success");
					
					redirect('admin/budgets', 'location');
					
				}
	}
	
	function company()
	{

		$header =
		 array( 
			  array("class"=>"left width200","name"=>"company_name","label"=>"ชื่อบริษัท"),
			  array("class"=>"left","name"=>"address","label"=>"ที่อยู่"),
			  array("class"=>"center edit","name"=>"company_id","label"=>"แก้ไข"),
			  array("class"=>"center delete","name"=>"company_id","label"=>"ลบ")
	 			);
		$rows = $this->db->query("select * from company")->result_array();
		$data["header"] = $header; 
		$data["rows"] = $rows;
		$this->template->load('admin/themes',"admin/company",$data);
	}
	function company_add()
	{
		$this->form_validation->set_message('required', 'กรุณาระบุ %s');
		$this->form_validation->set_rules('company_name',  'ชื่อบรริษัท', 'trim|required');
		$this->form_validation->set_rules('address',  '', 'trim');
		$this->form_validation->set_rules('tel',  '', 'trim');
		$this->form_validation->set_rules('fax',  '', 'trim');
		$this->form_validation->set_rules('email',  '', 'trim');
		
		
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->template->load('admin/themes',"admin/company_add",null);
		}
		else
		{ 	
			$this->db->insert("company",$_POST);
			$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
			
			redirect('admin/company', 'location');
			
		}
	}
	function company_edit($id)
	{
		$this->form_validation->set_message('required', 'กรุณาระบุ %s');
		$this->form_validation->set_rules('company_name',  'ชื่อบริษัท', 'trim|required');
		$this->form_validation->set_rules('address',  '', 'trim');
		$this->form_validation->set_rules('tel',  '', 'trim');
		$this->form_validation->set_rules('fax',  '', 'trim');
		$this->form_validation->set_rules('email',  '', 'trim');
		
		
		$this->db->where("company_id",$id);
		if ($this->form_validation->run() == FALSE)
		{
			$rows = $this->db->get("company")->result_array();
			$data["row"] = $rows[0];
			$this->template->load('admin/themes',"admin/company_edit",$data);
		}
		else
		{ 	
			
			$this->db->update("company",$_POST);
			
			$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");
			
			redirect('admin/company', 'location');
			
		}
	}
	function company_delete($id)
	{

		$this->form_validation->set_rules('company_id',  '', 'trim');


		$this->db->where("company_id",$id);
		if ($this->form_validation->run() == FALSE)
		{
			$rows = $this->db->get("company")->result_array();
			$data["row"] = $rows[0];
			$this->template->load('admin/themes',"admin/company_delete",$data);
		}
		else
		{

			$this->db->delete("company",$_POST);

			$this->user->set_message("","ลบเรียบร้อยแล้ว","success");

			redirect('admin/company', 'location');

		}
	}
	function file_filter()
	{
		$id = $_POST["file_category_id"];
		$filter = $this->session->userdata("filter");
		$filter["file_category_id"] = $id;
		
		
		$this->session->set_userdata("filter",$filter);
		redirect('admin/files', 'location');
	}
	function files($offset = 0 )
	{
		$filter = $this->session->userdata("filter");
		$condition = "";
		if($filter["file_category_id"])
		{
			$condition = " and f.file_category_id = ".$filter["file_category_id"];
		
		}
		$sql = "select * from files f left join  file_cats c on f.file_category_id = c.file_category_id   where 1 $condition order by file_id desc "; 
		$base = "admin/filse";
		
		$this->_page_query($sql,$base,"admin/files",$offset,array(),true,50);
		
		
		//$this->template->load('admin/themes',$this->user->current_controller(),null);
	}
	function file_add()
	{
		
		
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png|pdf|swf|zip|exe';
		$config['max_size']	= '1000000';
		$config['max_width']  = '10240';
		$config['max_height']  = '7680';
		//$config['file_name'] = mktime() ;
		$config["encrypt_name"] = true;
		$this->load->library('upload', $config);
		
		
		
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			
			$this->form_validation->set_rules('detail', 'อธิบายไฟล์', 'trim');
			$this->form_validation->set_rules('file_category_id', 'กลุ่มไฟล์', 'trim');
			if ($this->form_validation->run() == FALSE)
			{
				$error = array('error' => $this->upload->display_errors());
				
				//$this->template->load('admin/themes',$this->user->current_controller(),$error);
			}
			else
			{ 	
				
			}
			
			if ( !$file = $this->upload->do_upload("file_field"))
			{
			$data["error"] = array('error' => $this->upload->display_errors());
			
		 	
			$this->template->load('admin/themes',$this->user->current_controller(),$data);
		}
		else
		{
			
			
			$upload_data =  $this->upload->data();
			$values = array();
			$values["file_name"] = $upload_data["file_name"];
			$values["detail"] = $_POST["detail"];
			$values["file_category_id"] = $_POST["file_category_id"];
			$values["file_type"] = $upload_data["file_type"];
			$values["raw_name"] = $upload_data["raw_name"];
			$values["is_image"] = $upload_data["is_image"];
			$values["file_size"] = $upload_data["file_size"];
			$values["file_ext"] = $upload_data["file_ext"];
			$values["client_name"] = $upload_data["client_name"];
			$data["upload_data"] = $upload_data;
			$this->db->insert("files",$values);
			//$this->template->load('admin/themes',"admin/file_view",$data);
			//$data = array('upload_data' => $this->upload->data());
			$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
		
			redirect('admin/files', 'location');
			
		}
				
				
			
		

		
		
	}
	
	function file_delete($id)
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('file_id',  '', 'trim|required');
			
			
			$data['file'] = $this->db->query("select * from files where file_id =$id")->result_array();
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',$this->user->current_controller(),$data);
			}
			else
			{ 	$this->load->helper('file');
				$path = "./uploads/".$data["file"][0]["file_name"];
				
				 @unlink($path);
				$this->db->where('file_id', $id);
				$this->db->delete('files');
				$this->user->set_message("","ลบเรียบร้อยแล้ว","success");	
				$this->fs->save_log();	
				redirect('admin/files', 'location');
				
			}
	}
	function  promotions()
	{
		$this->template->load('admin/themes',$this->user->current_controller(),null);
	}
	function users()
	{
		$this->template->load('admin/themes',"admin/users",null);
	}
	function setting()
	{
		$this->template->load('admin/themes',"admin/users",null);
	}
	function front()
	{
			$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('home_bottom_bar_th',  '', 'trim');
			$this->form_validation->set_rules('home_bottom_bar_th', '', 'trim');
			$this->form_validation->set_rules('carpark_flash',  '', 'trim');
			$this->form_validation->set_rules('access_control', '','trim');
			$this->form_validation->set_rules('home_auto_mation',  '', 'trim');
			$this->form_validation->set_rules('home_bottom_bar_th', '','trim');
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->template->load('admin/themes',$this->user->current_controller(),null);
			}
			else
			{ 	
				
				foreach($_POST as $key => $row)
				{
					$this->db->where("option_name",$key);
					$this->db->update("options",array( "option_value" =>$row ));
					$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
					
				}
				redirect('admin/front', 'location');
				//print_r($_POST);
				//$this->db->insert("material_categories",$_POST);
				//$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
				//redirect('admin/categories', 'location');
				
			}
			
		
	}
	function promotion_add()
	{
	$this->template->load('admin/themes',"admin/dashboard",null);
	}
	function downloads($offset = 0)
	{
		
		$sql = "select * from downloads"; 
		$base = "admin/download";
		
		$this->_page_query($sql,$base,"admin/downloads",$offset,array(),true,50);
	}
	function pages()
	{
		$this->template->load('admin/themes',$this->user->current_controller(),null);
	}
	function page_edit($id)
	{
	$this->form_validation->set_message('required', 'กรุณาระบุ %s');
			$this->form_validation->set_rules('name',  '', 'trim|required');
			$this->form_validation->set_rules('keyword', '', 'trim');
			$this->form_validation->set_rules('is_publish', '', 'trim');
			$this->form_validation->set_rules('body_th',  '', 'trim');
			$this->form_validation->set_rules('body_en', '', 'trim');
			$this->form_validation->set_rules('title_th', '', 'trim');
			$this->form_validation->set_rules('title_en', '', 'trim');
			$this->form_validation->set_rules('thumbnail', '', 'trim');
			$this->form_validation->set_rules('is_publish', '', 'trim');
			
			
			if ($this->form_validation->run() == FALSE)
			{
				$this->db->where("page_id" , $id);
				$data["page"] =  $this->db->get("pages")->result_array();
				$this->template->load('admin/themes',$this->user->current_controller(),$data);
			}
			else
			{ 	
				$this->db->where("page_id" , $id);
				$this->db->update("pages",$_POST);
				$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");	
				redirect('admin/pages', 'location');
				
			}
	
	}
	
	function code()
	{
			
				print $this->p->getcode();
			
	}
    function user_roles()
	{

		$this->template->load('admin/themes',"admin/user_roles",null);
	}
	function  user_role_add()
	{
        $this->form_validation->set_rules('role_name',  'ชื่อกลุ่มผู้ใช้งาน', 'trim|required');

        $this->form_validation->set_rules('controllers[]',  'Action', 'trim|required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->template->load('admin/themes',"admin/user_role_add",null);
        }
        else
        {


             $values = array();
             $values["role_name"] = $_POST["role_name"];

             $values["controller_access"]  = implode(",", $_POST["controllers"]);



        $this->db->insert("roles",$values);
        $this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");

        redirect('admin/user_roles', 'location');

        }
	}
	function user_role_edit($id)
	{
        $this->form_validation->set_rules('role_name',  'ชื่อกลุ่มผู้ใช้งาน', 'trim|required');

        $this->form_validation->set_rules('controllers[]',  'Action', 'trim|required');

        $this->db->where("role_id",$id);
        if ($this->form_validation->run() == FALSE)
        {
            $rows = $this->db->get("roles")->result_array();
            $data["role"] = $rows[0];
            $this->template->load('admin/themes',"admin/user_role_edit",$data);
        }
        else
        {


         $values = array();
         $values["role_name"] = $_POST["role_name"];

         $values["controller_access"]  = implode(",", $_POST["controllers"]);
       

			$this->db->update("roles",$values);
			$this->user->set_message("","บันทึกเรียบร้อยแล้ว","success");

			redirect('admin/user_roles', 'location');

			}
	}
	function role_delete($id)
	{

        $this->form_validation->set_rules('role_id',  '', 'trim|required');



        $this->db->where("role_id",$id);
        if ($this->form_validation->run() == FALSE)
        {
            $rows = $this->db->get("roles")->result_array();
            $data["role"] = $rows[0];
            $this->template->load('admin/themes',"admin/user_role_delete",$data);
        }
        else
        {

			$this->db->delete("roles");
			$this->user->set_message("","ลบเรียบร้อยแล้ว","success");
			redirect('admin/user_roles', 'location');

			}
	}
	function inventory_list()
	{
		$this->template->load('admin/themes',"admin/inventory_list",null);
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */